﻿using System;
using System.Linq.Reactive;

namespace MathService.DataGenericSources
{
    public abstract class DataHost : IDisposable
    {
        /* -------------------------------------------------------------------------------- */

        public virtual void Dispose() { }

        /* -------------------------------------------------------------------------------- */
    }

    public abstract class DataHost<T> : DataHost, IObservableEx<T>
    {
        /* -------------------------------------------------------------------------------- */


        /* -------------------------------------------------------------------------------- */

        public event EventHandler Updated;

        protected virtual void OnUpdated(EventArgs args)
        {
            Updated.Start(this, args);
            f_ObservableObject.OnNext(args is EventArgs<T> ? ((EventArgs<T>)args).Argument : default(T));
        }

        /* -------------------------------------------------------------------------------- */

        protected DataHost() { }

        /* -------------------------------------------------------------------------------- */

        public virtual void Update() { OnUpdated(EventArgs.Empty); }


        /* -------------------------------------------------------------------------------- */

        #region Implementation of IObservableEx<T>

        private SimpleObservableEx<T> f_ObservableObject;

        /// <summary>Метод получения наблюдателя</summary>
        /// <returns>Наблюдатель объекта</returns>
        public virtual IObserverEx<T> GetObserver() { return (f_ObservableObject ?? (f_ObservableObject = new SimpleObservableEx<T>())).GetObserver(); }

        #endregion

        /* -------------------------------------------------------------------------------- */
    }
}
